package com.leedsoft.news2.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import com.leedsoft.news2.R;
import com.leedsoft.news2.fragment.LiveFragment;
import com.leedsoft.news2.fragment.MyFragment;
import com.leedsoft.news2.fragment.NewsFragment;
import com.leedsoft.news2.fragment.RecommendFragment;

//这是git提交的22222！
public class MainActivity extends FragmentActivity implements View.OnClickListener{
	/**
	 * 展示新闻
	 */
	private NewsFragment newsFragment;

	/**
	 * 展示推荐
	 */
	private RecommendFragment recommendFragment;
	/**
	 * 展示直播
	 */
	private LiveFragment liveFragmet;
	/**
	 * 展示我
	 */
	private MyFragment myFragment;
	/**
	 * 新闻界面布局
	 */
	private View newsLayout;

	/**
	 * 推荐布局界面
	 */
	private View recommendLayout;
	/**
	 * 直播布局界面
	 */
	private View liveLayout;
	/**
	 * 我的布局界面
	 */
	private View myLayout;
	/**
	 * tab显示新闻图标控件
	 */
	private ImageView newsImageView;

	/**
	 * tab显示推荐图标控件
	 */
	private ImageView recommendImageView;
	/**
	 * tab显示直播图标控件
	 */
	private ImageView liveImageView;
	/**
	 * tab显示我图标直播
	 */
	private ImageView myImageView;
	/**
	 * tab显示新闻文字控件
	 */
	private TextView newsTextView;

	/**
	 * tab显示推荐文字控件
	 */
	private TextView recommendTextView;
	/**
	 * tab显示直播文字控件
	 */
	private TextView liveTextView;
	/**
	 * tab显示我文字直播
	 */
	private TextView myTextView;
	/**
	 * 用于对Fragment管理
	 */
	private FragmentManager fragmentManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		/**
		 * 初始化控件
		 */
		initViews();
		fragmentManager = getSupportFragmentManager();
		//第一次启动时第0个tab
		setTabSelection(0);


		//加载新闻列表
//		loadHotNewsList();


	}


	private void initViews() {
		//布局初始化
		newsLayout = findViewById(R.id.news_layout);
		recommendLayout = findViewById(R.id.reconnend_layout);
		liveLayout = findViewById(R.id.live_layout);
		myLayout = findViewById(R.id.my_layout);

		//图片初始化
		newsImageView = (ImageView) findViewById(R.id.news_image);
		recommendImageView = (ImageView) findViewById(R.id.reconnend_image);
		liveImageView = (ImageView) findViewById(R.id.live_image);
		myImageView = (ImageView) findViewById(R.id.my_image);

		//文字控件初始化
		newsTextView = (TextView) findViewById(R.id.news_text);
		recommendTextView = (TextView) findViewById(R.id.reconnend_text);
		liveTextView = (TextView) findViewById(R.id.live_text);
		myTextView = (TextView) findViewById(R.id.my_text);

		//给Layout设置监听器
		newsLayout.setOnClickListener(this);
		recommendLayout.setOnClickListener(this);
		liveLayout.setOnClickListener(this);
		myLayout.setOnClickListener(this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_main, menu);
		return true;

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()){
			case R.id.news_layout:
				//当点击了新闻tab时，选中了第1个tab
				setTabSelection(0);
				break;
			case R.id.reconnend_layout:
				//当点击了推荐tab时，选中了第1个tab
				setTabSelection(1);
				break;
			case R.id.live_layout:
				//当点击了直播tab时，选中了第1个tab
				setTabSelection(2);
				break;
			case R.id.my_layout:
				//当点击了我的tab时，选中了第1个tab
				setTabSelection(3);
				break;
		}

	}

	/**
	 * 根据传入的index的参数来设定tab页
	 * @param index
	 *
	 * 给个tab对应的下标，0表示新闻，1表示推荐，2表示直播，3表示我的
	 */
	private void setTabSelection(int index){
		//每次选中之前先清除上次选中的状态
		clearSelection();
		//开启一个fragment事务
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		//先隐藏所有的Fragment,以防止多个Fragment同时显示在界面
		hideFragmens(transaction);
		switch (index) {
			case 0:
				//当点击新闻tab时，改变图片和文字的颜色
				newsImageView.setImageResource(R.drawable.message_selected);
				newsTextView.setTextColor(Color.WHITE);

				//如果MessageFragemnt为空，则创建个添加到界面
				if (newsFragment == null) {
					newsFragment = new NewsFragment();
					transaction.add(R.id.content, newsFragment);
				}else{
					//如果MessageFragment不为空，则直接显示
					transaction.show(newsFragment);
				}
				break;
			case 1:
				//当点击推荐tab时，改变图片和文字的颜色
				recommendImageView.setImageResource(R.drawable.contacts_selected);
				recommendTextView.setTextColor(Color.WHITE);

				//如果MessageFragment为空，则创建个添加到界面
				if(recommendFragment ==null){
					recommendFragment = new RecommendFragment();
					transaction.add(R.id.content,recommendFragment);
				}else{
					//如果MessageFragment不为空，则直接显示
					transaction.show(recommendFragment);
				}
				break;
			case 2:
				//当点击直播tab时，改变图片和文字的颜色
				liveImageView.setImageResource(R.drawable.news_selected);
				liveTextView.setTextColor(Color.WHITE);

				//如果MessageFragment为空，则创建个添加到界面
				if(liveFragmet == null){
					liveFragmet = new LiveFragment();
					transaction.add(R.id.content,liveFragmet);
				}else{
					//如果MessageFragment不为空，则直接显示
					transaction.show(liveFragmet);

				}
				break;
			case 3:
				myImageView.setImageResource(R.drawable.setting_selected);
				myTextView.setTextColor(Color.WHITE);

				//如果MessageFragment为空，则创建个添加到界面
				if(myFragment == null){
					myFragment = new MyFragment();
					transaction.add(R.id.content,myFragment);
				}else{
					//如果MessageFragment不为空，则直接显示
					transaction.show(myFragment);
				}
				break;
		}
		transaction.commit();
	}

	private void clearSelection(){
		newsImageView.setImageResource(R.drawable.message_unselected);
		newsTextView.setTextColor(Color.parseColor("#82858b"));

		recommendImageView.setImageResource(R.drawable.contacts_unselected);
		recommendTextView.setTextColor(Color.parseColor("#82858b"));

		liveImageView.setImageResource(R.drawable.news_unselected);
		liveTextView.setTextColor(Color.parseColor("#82858b"));

		myImageView.setImageResource(R.drawable.setting_unselected);
		myTextView.setTextColor(Color.parseColor("#82858b"));

	}
	private void hideFragmens(FragmentTransaction transaction){
		if(newsFragment !=null){
			transaction.hide(newsFragment);
		}
		if(recommendFragment !=null){
			transaction.hide(recommendFragment);
		}
		if(liveFragmet !=null){
			transaction.hide(liveFragmet);
		}
		if(myFragment !=null){
			transaction.hide(myFragment);
		}
	}

}
